<div id="content" class="container_12">
<form action="<?php echo base_url().'account/updateProfile'; ?>" method="POST" enctype="multipart/form-data">
    <?php
    
    if(!isset($profile_id) OR !isset($fname) OR !isset($lname) OR !isset($gender) OR !isset($birthday)) {
        echo '<div class="error align-center">Account not recognized. Given data might be invalid or missing.</div>';
    } else {
    ?>
    <div class="grid_3">
        <?php
        
        if($avatar != null) {
            echo '<img src="'.base_url().'account/getavatar/'.$_GET['id'].'" alt="avatar" />';
        } else {
            echo '<img src="'.base_url().'images/unknown_user.jpg" alt="avatar" />';
        }
        
        ?>
        <input type="file" name="avatar" accept="image/*" class="form-file" />
    </div>
    <div class="grid_9">
        <div class="header-border">
            <input type="button" name="edit" value="Edit Profile" class="silver-button float-right" disabled />
            <input type="submit" name="save" value="Save Profile" class="silver-button float-right" />
            <h2>Edit Profile [ <?php echo $fname." ".$lname; ?> ]</h2>
        </div>
        <?php
        
        if(isset($_GET['err'])) {
            echo '
            <div class="error"><img src="'.base_url().'images/error.png" alt="success" class="valign-middle" />
            <span class="valign-middle">
            ';
            switch($_GET['err']) {
                case 1:
                    echo 'Please fill in all required fields!';break;
                case 2:
                    echo 'Failure in confirming the new password!';break;
                case 3:
                    echo 'Provided &quot;Old Password&quot; seems to be invalid!';break;
                case 4:
                    echo 'Something went wrong. Please try again.';break;
                case 5:
                    echo 'The username chosen is unavailable!';break;
                case 6:
                    echo 'Username and Password must be between 6-32 characters!';break;
                case 7:
                    echo 'Invalid image file. Avatar can only be jpg, png, gif!';
            }
            echo '
            </span>
            </div><br />';
        }

        $privileges = getprivileges($this->session->userdata('role_id'));
        
        ?>
        <fieldset>
        <legend>Account Information</legend>
        <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
        <input type="hidden" name="oUsername" value="<?php echo $username; ?>" />
        <table class="tbl">
            <tr>
                <td class="tbl-td-a align-right"><span>*Username</span></td>
                <td><input type="text" name="username" value="<?php echo $username; ?>" class="form-input" required /></td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span id="passwordSpan">Password</span></td>
                <td>
                    <input type="checkbox" name="editPassword" id="editPassword" class="valign-middle" /><span class="valign-middle">Change Password</span><br />
                <?php
                
                if($_GET['id'] == $this->session->userdata('account_id')) {
                    echo '<input type="password" name="oldPassword" placeholder="Old Password" id="oldPassword" class="form-input" disabled />';
                }
                
                ?>
                    <input type="password" name="newPassword" placeholder="New Password" id="newPassword" class="form-input" disabled />
                    <input type="password" name="conPassword" placeholder="Confirm Password" id="conPassword" class="form-input" disabled />
                    <script type="text/javascript" src="<?php echo base_url().'js/profile.js'; ?>"></script>
                </td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span>*Account Role</span></td>
                <td>
                    <select name="role" class="form-select"<?php if($this->session->userdata('role_id') != 1){echo ' disabled';} ?> required>
                    <option value="">--Choose Role--</option>
                    <?php
                    
                    foreach($roles as $role) {
                        echo '<option value="'.$role->role_id.'"';
                        if($role_id == $role->role_id){echo ' selected';}
                        echo '>'.$role->role_name.'</option>';
                    }
                    
                    ?>
                    </select>
                    <?php
                    if($this->session->userdata('role_id') != 1){echo '<input type="hidden" name="role" value="'.$role_id.'">';}
                    ?>
                </td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span>*Account Status</span></td>
                <td>
                    <select name="status" class="form-select"<?php if(!in_array(6,$privileges)){echo ' disabled';} ?> required>
                        <option value="">--Choose Status--</option>
                        <option value="1"<?php if($status == 1){echo ' selected';} ?>>Active</option>
                        <option value="0"<?php if($status == 0){echo ' selected';} ?>>Inactive</option>
                    </select>
                    <?php
                    if(!in_array(6,$privileges)){echo '<input type="hidden" name="status" value="'.$role_id.'">';}
                    ?>
                </td>
            </tr>
        </table>
        </fieldset>
        <br />
        <fieldset>
        <legend>Profile Information</legend>
        <table class="tbl">
            <tr>
                <td class="tbl-td-a align-right"><span>*First Name</span></td>
                <td><input type="text" name="fname" value="<?php echo $fname; ?>" class="form-input"<?php if(!in_array(6,$privileges)){echo ' readonly';} ?> required /></td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span>*Last Name</span></td>
                <td><input type="text" name="lname" value="<?php echo $lname; ?>" class="form-input"<?php if(!in_array(6,$privileges)){echo ' readonly';} ?> required /></td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span>*Gender</span></td>
                <td>
                    <input type="radio" name="gender" value="Male" class="valign-middle"<?php if($gender=="Male"){echo " checked";}else if(!in_array(6,$privileges)){echo " disabled";} ?> /><span class="valign-middle">Male</span>
                    <input type="radio" name="gender" value="Female" class="valign-middle"<?php if($gender=="Female"){echo " checked";}else if(!in_array(6,$privileges)){echo " disabled";} ?>  /><span class="valign-middle">Female</span>
                </td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span>*Birthday</span></td>
                <td><input type="text" name="birthday" value="<?php echo $birthday; ?>" class="form-input datepicker"<?php if(!in_array(6,$privileges)){echo ' readonly';} ?> required /></td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span>*Address</span></td>
                <td><input type="text" name="address" value="<?php echo $address; ?>" class="form-input" required /></td>
            </tr>
            <tr>
                <td class="tbl-td-a align-right"><span>About Me</span></td>
                <td><textarea name="about" class="form-input" rows="5"><?php echo nl2br($about); ?></textarea></td>
            </tr>
        </table>
        </fieldset>
    </div>
    <?php } ?>
</form>
</div>
<script type="text/javascript" src="<?php echo base_url(); ?>js/datepicker.js"></script>